package jinyilw.swing.visuals.panel;

import jinyilw.swing.Alignment;

import javax.swing.JComponent;
import java.awt.Component;

public class PositionPanel extends JComponent // 自定义流式面板
{
	private static final long serialVersionUID = 6091537210345683699L;
	private Alignment align = Alignment.CENTER;
	private PositionProducer positionProducer;

	public PositionPanel(PositionProducer positionProducer)
	{
		this.positionProducer = positionProducer;
		setLayout(null);
	}

	public PositionPanel(Alignment align)
	{
		setLayout(null);
		this.align = align;
	}

	public void setAlignment(Alignment align)
	{
		this.align = align;
		updateComps();
	}

	@Override
	public Component add(Component comp)
	{
		super.add(comp);
		updateComps();
		return comp;
	}

	@Override
	public Component add(Component comp, int index)
	{
		super.add(comp, index);
		updateComps();
		return comp;
	}

	@Override
	public void remove(int index)
	{
		Component[] comps = getComponents();
		if (index < 0 || index >= comps.length)
			return;
		super.remove(index);
		updateComps();
	}

	public void updateComps()
	{
		Component[] comps = getComponents();
		if (comps == null)
		{
		}

	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		boolean unResized = width == getWidth() && height == getHeight();
		super.setBounds(x, y, width, height);
		if (unResized)
			return;
		if (positionProducer == null)
			return;
		positionProducer.calculateMaxColumn(width, 0);
	}
}
